package site.shihuan.helpdesk.service.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import site.shihuan.helpdesk.common.model.RestResult;
import site.shihuan.helpdesk.common.model.RestResultUtils;
import site.shihuan.helpdesk.security.context.UserContext;
import site.shihuan.helpdesk.service.manager.VisitorManager;
import site.shihuan.helpdesk.service.model.po.SvcVisitorPO;

import java.util.List;

/**
 * 访客API
 *
 * @author 周世焕
 * @Date 2021/3/2 22:24
 */
@RestController
@RequestMapping("/v1/service/visitor")
@Slf4j
public class VisitorController {

    @Autowired
    private VisitorManager visitorManager;

    /**
     * 查询访客信息
     * @param visitorId
     */
    @GetMapping("info")
    public RestResult findVisitorInfo(String visitorId, String tenantId) {
        SvcVisitorPO visitorPO = visitorManager.findVisitor(visitorId,tenantId);
        if(visitorPO == null) {
            visitorPO = visitorManager.createVisitor(visitorId,tenantId);
        }
        return RestResultUtils.success(visitorPO);
    }

    /**
     * 查询在线的访客列表
     */
    @GetMapping("online-list")
    public RestResult findWaitingVisitors() {
        String tenantId = UserContext.getUser().getTenantId();
        return RestResultUtils.success(visitorManager.findWaitingVisitors(tenantId));
    }

    /**
     * 查询访客列表
     */
    @GetMapping("list")
    public RestResult findVisitors() {
        String tenantId = UserContext.getUser().getTenantId();
        return RestResultUtils.success(visitorManager.findVisitors(tenantId));
    }

    /**
     * 完善访客信息
     * @param visitorPO
     * @return
     */
    @PutMapping("info")
    public RestResult updateVisitorInfo(@RequestBody SvcVisitorPO visitorPO) {
        return RestResultUtils.success(visitorManager.updateVisitor(visitorPO));
    }
}
